package com.csu.jpetstore_reconfiguration_back.util;

import com.aliyun.oss.ServiceException;
import com.csu.jpetstore_reconfiguration_back.common.CommonResponse;
import jakarta.validation.ConstraintViolationException;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.method.annotation.HandlerMethodValidationException;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public CommonResponse<Object> handleMissingServletRequestParameterException(MissingServletRequestParameterException e) {
        logger.error("{} \n {}", e.getMessage(), e.getClass());
        return CommonResponse.createForError("缺少参数");
    }

    @ExceptionHandler(ConstraintViolationException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public CommonResponse<Object> handleConstraintViolationException(ConstraintViolationException e) {
        logger.error("{} \n {}", e.getMessage(), e.getClass());
        return CommonResponse.createForError(e.getMessage());
    }
    @ExceptionHandler(HttpMessageNotReadableException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public CommonResponse<Object> handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
        logger.error("{} \n {}", e.getMessage(), e.getClass());
        return CommonResponse.createForError("数据格式错误");
    }
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public CommonResponse<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        logger.error("{} \n {}", e.getMessage(), e.getClass());
        return CommonResponse.createForError("数据不完整");
    }

    @ExceptionHandler(HandlerMethodValidationException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public CommonResponse<Object> handleHandlerMethodValidationException(HandlerMethodValidationException e) {
        logger.error("{} \n {}", e.getMessage(), e.getClass());
        return CommonResponse.createForError("数据有误");
    }

    //没有token
    @ExceptionHandler(ServiceException.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public CommonResponse<Object> handleHandlerServiceException(ServiceException e) {
        logger.error("{} \n {}", e.getMessage(), e.getClass());
        return CommonResponse.createForError(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    @ResponseBody
    public CommonResponse<Object> handleException(Exception e) {
        logger.error("{} \n {}", e.getMessage(), e.getClass());
        return CommonResponse.createForError("服务器错误");
    }


}
